﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsCatchParse
    {
        private readonly KeRui.DAL.CmsCatchParse dal = new KeRui.DAL.CmsCatchParse();

        public int Add(KeRui.Model.CmsCatchParse model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsCatchParse> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsCatchParse> list = new List<KeRui.Model.CmsCatchParse>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsCatchParse item = new KeRui.Model.CmsCatchParse();
                    if (dt.Rows[i]["ParseID"].ToString() != "")
                    {
                        item.ParseID = int.Parse(dt.Rows[i]["ParseID"].ToString());
                    }
                    item.ParseName = dt.Rows[i]["ParseName"].ToString();
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        item.IsUsing = int.Parse(dt.Rows[i]["IsUsing"].ToString());
                    }
                    item.ListParseBegin = dt.Rows[i]["ListParseBegin"].ToString();
                    item.ListParseEnd = dt.Rows[i]["ListParseEnd"].ToString();
                    item.ListParseTags = dt.Rows[i]["ListParseTags"].ToString();
                    item.ListParseAttrs = dt.Rows[i]["ListParseAttrs"].ToString();
                    item.ListParseRule = dt.Rows[i]["ListParseRule"].ToString();
                    item.ContentUrl = dt.Rows[i]["ContentUrl"].ToString();
                    item.ContentParseBegin = dt.Rows[i]["ContentParseBegin"].ToString();
                    item.ContentParseEnd = dt.Rows[i]["ContentParseEnd"].ToString();
                    item.ContentParseTags = dt.Rows[i]["ContentParseTags"].ToString();
                    item.ContentParseAttrs = dt.Rows[i]["ContentParseAttrs"].ToString();
                    item.ContentParseRule = dt.Rows[i]["ContentParseRule"].ToString();
                    item.ContentPageParseRule = dt.Rows[i]["ContentPageParseRule"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    item.ListEncoding = dt.Rows[i]["ListEncoding"].ToString();
                    item.ContentEncoding = dt.Rows[i]["ContentEncoding"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int ParseID)
        {
            return this.dal.Delete(ParseID);
        }

        public bool Exists(int ParseID)
        {
            return this.dal.Exists(ParseID);
        }

        public List<KeRui.Model.CmsCatchParse> GetList()
        {
            string strWhere = "";
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchParse", -1, -1, strWhere, "ParseID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public KeRui.Model.CmsCatchParse GetModel(int ParseID)
        {
            return this.dal.GetModel(ParseID);
        }

        public KeRui.Model.CmsCatchParse GetModelByCache(int ParseID)
        {
            string cacheKey = "CmsCatchParseModel-" + ParseID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ParseID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsCatchParse) cache;
        }

        public DataSet GetSelectList()
        {
            string strWhere = "";
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsCatchParse", strWhere, "ParseID Desc", " ParseID,ParseName");
        }

        public DataSet GetSelectList(int IsUsing)
        {
            string strWhere = "IsUsing=" + IsUsing;
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsCatchParse", strWhere, "ParseID Desc", " ParseID,ParseName");
        }

        public int Update(KeRui.Model.CmsCatchParse model)
        {
            return this.dal.Update(model);
        }
    }
}

